<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.value"
          clearable
          placeholder="输入搜索内容"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="800px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <!--<el-form-item label="id">-->
          <!--<el-input v-model="form.id" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <!--<el-form-item label="cid" prop="cid">-->
          <!--<el-input v-model="form.cid" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <el-form-item label="应急演练标题" prop="exerciseTitle">
            <el-input v-model="form.exerciseTitle" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="演练目的">
            <el-input v-model="form.exerciseObjective" type="textarea" style="width: 370px;" />
          </el-form-item>

          <!--<el-form-item label="演练日期" prop="exerciseDate">-->
          <!--<el-date-picker-->
          <!--v-model="form.exerciseDate"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--type="datetime"/>-->
          <!--</el-form-item>-->

          <el-form-item label="演练日期" prop="exerciseDate">
            <el-date-picker v-model="form.exerciseDate" type="date" value-format="yyyy-MM-dd" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="演练流程">
            <el-input v-model="form.exerciseProcess" type="textarea" style="width: 370px;" />
          </el-form-item>
          <!--<el-form-item label="公告通知状态" prop="isNotive">-->
          <!--<el-input v-model="form.isNotive" style="width: 370px;" />-->
          <!--</el-form-item>-->

          <el-form-item label="公告通知状态" prop="isNotive">
            <el-radio-group v-model="form.isNotive" style="width: 178px">
              <el-radio label="关闭">关闭</el-radio>
              <el-radio label="开启">开启</el-radio>

            </el-radio-group>

          </el-form-item>

          <el-form-item v-if="form.isNotive!='关闭'" label="通知提前小时">

            <el-input
              v-model="form.advanceHour"
              maxlength="“8”"
              oninput="value=value.replace(/[^\d.]/g,'')"
              style="width: 170px;"
            />

          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <!--<el-table-column prop="id" label="id" />-->
        <!--<el-table-column prop="cid" label="cid" />-->
        <el-table-column prop="exerciseTitle" label="应急演练标题" />
        <el-table-column prop="exerciseObjective" label="演练目的" />
        <el-table-column prop="isNotive" label="公告通知状态" />

        <el-table-column prop="exerciseDate" label="演练日期" />

        <el-table-column prop="exerciseProcess" label="演练流程" />
        <el-table-column prop="advanceHour" label="通知提前小时" />
        <el-table-column
          v-permission="['admin','exercise:edit','exercise:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudExercise from '@/api/emergency/exercise'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  id: null,
  cid: null,
  exerciseObjective: null,
  exerciseProcess: null,
  isNotive: '关闭',
  exerciseDate: null,
  advanceHour: null,
  exerciseTitle: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'Exercise',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '应急演练', url: 'api/exercise', sort: 'id,desc', crudMethod: { ...crudExercise }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'exercise:add'],
        edit: ['admin', 'exercise:edit'],
        del: ['admin', 'exercise:del']
      },
      rules: {
        cid: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        isNotive: [
          { required: true, message: '公告通知状态不能为空', trigger: 'blur' }
        ],
        exerciseDate: [
          { required: true, message: '演练时间不能为空', trigger: 'blur' }
        ],
        exerciseTitle: [
          { required: true, message: '应急演练标题不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'isNotive', display_name: '公告通知状态' },
        { key: 'exerciseDate', display_name: '演练时间' },
        { key: 'exerciseTitle', display_name: '应急演练标题' }
      ]
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    }
  }
}
</script>

<style scoped>

</style>
